<?php
namespace Common\Controller;

use Common\Controller\BaseController;
use Common\Model\CarModel;
use Common\Model\CheckModel;
use Common\Model\FireModel;
use Common\Model\PermitModel;
use Common\Model\StaffModel;
use PHPExcel_Writer_Excel5;

class DataController extends BaseController{
    static $action = array(
        'car'=>'机动车申请记录',
        'check'=>'常规安全检查',
        'permit'=>'动火证申请记录',
        'fire'=>'消防器材申请',
//        'report'=>'',
        'staff'=>'教职工',
    );
    static $time   = array(
        'car'=>'apply_time',
        'check'=>'check_time',
        'fire'=>'apply_time',
        'report'=>'reg_time',
    );
    static $car = array(
        'tab_car_id'=>'机动车申请编号',
        'apply_time'=>'申请时间',
        'status'=>'状态',
        'staff_id'=>'教职工工号',
        'car_type'=>'车证类型',
        'owner_name'=>'所有人名字',
        'owner_add'=>'所有人地址',
        'relation'=>'与车主关系',
        'brand'=>'品牌',
        'color'=>'颜色',
        'xingshi_license'=>'驾驶证照片',
        'jiashi_license'=>'行驶证照片',
        'car_photo'=>'车照片',
        'plate_number'=>'车牌号',
    );
    static $check = array(
        'tab_check_id'=>'安全检查编号',
        'room_id'=>'检查单位编号',
        'check_content'=>'检查内容',
        'check_time'=>'检查时间',
        'status' =>'状态',
        'remark' =>'保卫处邮件备注',
    );
    static $permit = array(
        'tab_permit_id' => '动火证申请编号',
        'apply_time' => '申请时间',
        'status' => '状态',
        'remark' => '邮件备注',
        'staff_id' => '教职工工号',
        'apply_name' => '申请动火的校内单位名称',
        'apply_incharge' => '申请动火单位负责人',
        'construct_name' => '施工单位名称',
        'construct_incharge' => '施工单位负责人',
        'special_cer' => '特种操作作业证',
        'special_id' => '特种作业操作证证件号码',
        'special_time' => '特种作业操作证有效期时间',
        'operator_name' => '动火操作人员',
        'operator_reason' => '动火操作原因',
        'operator_localtion' => '动火操作部位及环境',
        'operator_photo' => '施工方案图',
        'operator_type' => '动火操作类型',
        'operator_starttime' => '动火操作开始时间',
        'operator_endtime' => '动火操作结束时间',
        'operator_measure' => '动火操作采取的安全措施'
    );
    static $fire = array(
        'tab_fire_id' => '消防器材申请编号',
        'apply_time' => '申请时间',
        'status' => '状态',
        'remark' => '保卫处备注，理由',
        'staff_id' => '教职工编号',
        'apply_unit' => '申请单位',
        'apply_content' => '配置申请内容',
        'powder_amount' => '干粉数量',
        'powder_location' => '干粉配置地点',
        'powder_configure' => '干粉配置时间',
        'powder_repair' => '干粉维修时间',
        'carbon_amount' => '二氧化碳数量',
        'carbon_location' => '二氧化碳配置地点',
        'carbon_configure' => '二氧化碳配置时间',
        'carbon_repair' => '二氧化碳维修时间',
        'blanket_amount' => '灭火毯数量',
        'blanket_location' => '灭火毯配置地点',
        'blanket_configure' => '灭火毯配置时间',
        'extinguisher_amount' => '灭火箱数量',
        'extinguisher_location' => '灭火箱配置地点',
        'extinguisher_cofigure' => '灭火箱配置时间',
    );
    static $staff = array(
        'staff_id'   => '教职工编号',
        'staff_name' => '教职工名字',
        'staff_unit' => '教职工单位',
        'staff_tel' => '联系电话',
        'staff_email' => '邮箱',
        'staff_ins' => '所属院系',
        'role_id' => '角色',
    );
    static $role = array(
        '1'=>'保卫处管理员',
        '2'=>'保卫处监控员',
        '3'=>'学院领导',
        '4'=>'普通教职工',
        '5'=>'超级管理员',
    );
    public function _initialize(){

//        parent::_initialize();
//        self::$_action_block = 1;
//        $this->assign('block', self::$_action_block);
    }
    public function export($action,$where){
        $data = $this->getData($action,$where);
        $this->export_data($action,$data);
    }
    protected function export_data($action,$data){
        Vendor('PHPExcel');
        Vendor('IOFactory');
        $title = self::$action[$action].date('YmdHi');
        $excel = new \PHPExcel();
        $excel->getProperties()->setTitle($title);
        $excel->setActiveSheetIndex(0);
        $i = 'A';
        foreach ($data['title'] as $v){
            $excel->setActiveSheetIndex(0)
                ->setCellValue($i++."1",$v);
        }
        foreach ($data['data'] as $key=>$value){
            $i = 'A';
            $c = $key+2;
            foreach ($value as $k=>$v){
                $excel->setActiveSheetIndex(0)->setCellValue(($i++).$c,$v);
            }
        }
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename='{$title}.xls'");
        header('Cache-Control: max-age=0');
        header('Cache-Control: max-age=1');

        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header ('Pragma: public'); // HTTP/1.0
        $objWriter=  new PHPExcel_Writer_Excel5($excel);
        $objWriter->save('php://output');
        exit;
    }
    protected function getData($action='',$where){
        $data = array();
        $where = '';
        if(array_key_exists($action,self::$action)){
            $where = $this->return_where($action,$where);
            $action = "get_".$action;
            $data = $this->$action($where);
        }
        return $data;
    }
    protected function return_where($action,$where){
        if(empty($where['s'])&&empty($where['e'])){
            return " 1 ";
        }else if(!empty($where['s'])&&empty($where['e'])){
            return " UNIX_TIMESTAMP(".$action.".".self::$time[$action].") <=".$where['s']." ";
        }else if(empty($where['s'])&&!empty($where['e'])){
            return " UNIX_TIMESTAMP(".$action.".".self::$time[$action].") >=".$where['e']." ";
        }else if(!empty($where['s'])&&!empty($where['e'])){
            return " UNIX_TIMESTAMP(".$action.".".self::$time[$action].") <=".$where['s']." and UNIX_TIMESTAMP(".$action.".".self::$time[$action].") >=".$where['e']." ";
        }
    }
    protected function get_car($where){
        $carModel = new CarModel();
        $ret = array('title'=>self::$car,'data'=>array());
        foreach (self::$car as $key=>$v){
            $fields[] = "car.".$key;
        }
        $fields = implode(',',$fields);
        $list = $carModel->field($fields)
            ->order('apply_time DESC')
            ->join('staff on staff.staff_id=car.staff_id','LEFT')
            ->where($where)
            ->select();
        if(!empty($list)){
            foreach ($list as &$v){
                $v['car_type'] = $v['car_type'] ==1 ? "通行证" : "停车证";
                $v['relation'] = $v['relation'] ==1 ? "本人" : "夫妻";
                $v['status']   = $v['status'] ==1 ? "待审核" : $v['status'] ==2? "已通过":"待整改";
            }
        }
        $ret['data'] = !empty($list) ? $list : array();
        return $ret;
    }
    protected function get_check($where){
        $checkModel = new CheckModel();
        $ret = array('title'=>self::$check,'data'=>array());
        foreach (self::$check as $key=>$v){
            $fields[] = "check.".$key;
        }
        $fields = implode(',',$fields);
        $list = $checkModel->field($fields)
            ->join('room on check.room_id=room.room_id','LEFT')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->where($where)
            ->order('check.id DESC')
            ->select();
        if(!empty($list)){
            foreach ($list as &$v){
                $v['status']   = $v['status'] ==1 ? "待审核" : $v['status'] ==2? "已通过":"未通过";
            }
        }
        $ret['data'] = !empty($list) ? $list : array();
        return $ret;
    }
    protected function get_permit($where){
        $permitModel = new PermitModel();
        $ret = array('title'=>self::$permit,'data'=>array());
        foreach (self::$permit as $key=>$v){
            $fields[] = "permit.".$key;
        }
        $fields = implode(',',$fields);
        $list = $permitModel->field($fields)
            ->order('apply_time DESC')
            ->join('staff on staff.staff_id=permit.staff_id','LEFT')
            ->where($where)
            ->select();
        if(!empty($list)){
            foreach ($list as &$v){
                $v['status']   = $v['status'] ==1 ? "已通过" : $v['status'] ==2? "已整改":"未通过";
                $v['special_cer']   = $v['special_cer'] ==1 ? "有" : "没有";
            }
        }
        $ret['data'] = !empty($list) ? $list : array();
        return $ret;
    }
    protected function get_fire($where){
        $fireModel = new FireModel();
        $ret = array('title'=>self::$fire,'data'=>array());
        foreach (self::$fire as $key=>$v){
            $fields[] = "fire.".$key;
        }
        $fields = implode(',',$fields);
        $list = $fireModel->field($fields)
            ->order('apply_time DESC')
            ->join('staff on staff.staff_id=fire.staff_id','LEFT')
            ->where($where)
            ->select();
        if(!empty($list)){
            foreach ($list as &$v){
                $v['status']   = $v['status'] ==1 ? "待审核" : $v['status'] ==2? "已通过":"未通过";
            }
        }
        $ret['data'] = !empty($list) ? $list : array();
        return $ret;
    }
    protected function get_staff(){
        $staffModel = new StaffModel();
        $ret = array('title'=>self::$staff,'data'=>array());
        foreach (self::$staff as $key=>$v){
            $fields[] = "staff.".$key;
        }
        $fields = implode(',',$fields);
        $list = $staffModel->getStaffList();
        if(!empty($list)){
            foreach ($list as &$v){
                $v['role_id']   = self::$role[$v['role_id']];
            }
        }
        $ret['data'] = !empty($list) ? $list : array();
        return $ret;
    }
}